package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.dao.UserDao;
import com.yituo.mjworld.mvc.entity.web.WebOrder;
import com.yituo.mjworld.mvc.entity.web.WebUserAddress;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class UserAddressServiceImpl extends BaseService {
  @Autowired
  UserDao userDao;


  /**
   * 获取用户的收获地址
   *
   * @param userId 欲获取的用户ID
   * @return
   */
  public WebUserAddress getUserAddress(String userId) {
    return userDao.getUserAddress(userId);
  }

  public WebUserAddress getUserAddress(int addressId, String userId) {
    return userDao.getUserAddress(addressId, userId);
  }

  @Override
  public JSONObject getList(QueryForm form) {
    String countSql = "select count(id) from web_user_address where userId=:userId";
    String sql = "select " +
        " id," +
        " userId," +
        " provice," +
        " city," +
        " area," +
        " address," +
        " postcode," +
        " contactTel," +
        " receiver," +
        " createDate," +
        " orderNum," +
        " isDefault " +
        " from web_user_address where userId=:userId";
    return get(sql, countSql, form, form.getParam());
  }

  /**
   * 删除收获地址
   *
   * @param form
   * @return
   */
  @Override
  public Result delete(DeleteForm form) {
    Result result = new Result();
    String sql = "delete from web_user_address where userId=:userId and id=:id";
    Map<String, Object> params = form.getParam();
    int i = baseDao
        .getSession()
        .createNativeQuery(sql)
        .setParameter("userId", params.get("userId"))
        .setParameter("id", params.get("id"))
        .executeUpdate();
    if (i == 1) {
      result.setResult(true);
    } else {
      result.setResult(false).setMsg("该条记录不存在!");
    }
    return result;
  }

  @Override
  public Result saveOrUpdate(BaseForm baseForm) {
    Result result = new Result();
    WebUserAddress vo1 = (WebUserAddress) baseForm;
    WebUserAddress vo2 = (WebUserAddress) baseDao.get(WebUserAddress.class, vo1.getId());
    if (vo2 == null) {
      vo1.setCreateDate(new Date());
      this.baseDao.save(vo1);
      result.setResult(true);
    } else {
      vo1.setCreateDate(vo2.getCreateDate());
      this.baseDao.merge(vo1);
      result.setResult(true);
    }
    return result;
  }

  /**
   * 设置为默认地址
   *
   * @return
   */
  public Result setDefault(String userId, int id) {
    Result result = new Result();
    WebUserAddress vo1 = (WebUserAddress) baseDao.get(WebUserAddress.class, id);
    if (vo1 == null) {
      return result.setResult(false).setMsg("当前收获地址不存在啊!");
    }
    if (userId.equals(vo1.getUserId())) {
      int i = this.userDao.updateUserAllAddress(userId);
      if (i == 0) {
        return result.setResult(false).setMsg("更新失败!");
      }
      vo1.setIsDefault(true);
      this.baseDao.saveOrUpdate(vo1);
      return result.setResult(true);
    } else {
      return result.setResult(false).setMsg("你无权修改该地址!");
    }
  }


}
